package mf.com.Service.impl;

import cn.hutool.core.date.DateUtil;
import io.minio.*;
import jakarta.annotation.Resource;
import mf.com.Service.FileUploadService;
import mf.com.exception.CustomExceptionHandler;
import mf.com.properties.MinioProperties;
import mf.com.vo.common.ResultCodeEnum;
import mf.com.vo.common.ViewContentTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: lmf
 * @Create: 2024/3/30 16:15
 * Description:  文件上传实现类
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Resource
    private MinioProperties minioProperties;

    @Override
    public String uploadFile(MultipartFile file) {
        System.out.println(file);
        System.out.println(file);

        try {
            // 创建MinioClient对象 getEndpointUrl
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperties.getEndpointUrl())

                            .credentials(minioProperties.getAccessKey(),
                                    minioProperties.getSecretKey())
                            .build();

            // 创建bucket
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            //获取上传文件名称
            // 1 每个上传文件名称唯一的   uuid生成 01.jpg
            //2 根据当前日期对上传文件进行分组 20230910

            // 20230910/u7r54209l097501.jpg
            // 获取当前日期
            String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
            // 生成uuid
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            // 将文件存放到 当前日期下名称为 uuid加上传名称
            String filename = dateDir+"/"+uuid+file.getOriginalFilename();

            // 文件上传
            minioClient.putObject(
                    PutObjectArgs.builder()
                            // 上传的bucket
                            .bucket(minioProperties.getBucketName())
                            .contentType(file.getContentType())
                            // 上传的文件名称(斜杠会被替换成目录)
                            .object(filename)
                            // 上传的文件内容
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .build());

            //获取上传文件在minio路径
            //http://127.0.0.1:9000/spzx-bucket/01.jpg
            return minioProperties.getEndpointUrl()+minioProperties.getBucketName()+"/"+filename;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomExceptionHandler(ResultCodeEnum.SYSTEM_ERROR);
        }
    }
}
